﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

using CE.Global;

public partial class management_User_carquotehistory_report : System.Web.UI.Page
{
    string PageCode = "CXTHRCQH";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();

            int _cryr = CE.Global.General.GetUTCDateTime().Year;
            int _styr = 2010;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
                ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");

            ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");

            if (Request.QueryString["ceid"] != null || Request.QueryString["cecarid"] != null)
            {
                trSearch.Visible = false;
                Button1_Click(this, null);
            }
            else
            {
                trSearch.Visible = true;
            }
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        txtInsuranceType.Text = "0";
        txtReportDate1.Text = " ";
        txtReportDate2.Text = " ";
        if (Request.QueryString["ceid"] != null)
        {
            txtCEID.Text = Request.QueryString["ceid"].ToString();
            ddlUserType.Text = "400";
            ddlReportType.Text = "0";
        }
        else
        {
            txtCEID.Text = "0";
        }
        if (Request.QueryString["cecarid"] != null)
        {
            txtCarId.Text = Request.QueryString["cecarid"].ToString();
        }
        else
        {
            txtCarId.Text = "0";
        }
        txtReportDate1.Text = "";
        txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
        txtReportDate1.Text = ddlYear1.Text;
        txtReportDate1.Text += ddlMonth1.Text;
        if (ddlReportType.SelectedIndex > 0)
        {
            if (ddlDay1.SelectedIndex >= 0)
            {
                txtReportDate1.Text += ddlDay1.Text;
            }
        }
        if (ddlReportType.SelectedIndex == 2)
        {
            txtReportDate2.Text = ddlYear2.Text;
            txtReportDate2.Text += ddlMonth2.Text;
            txtReportDate2.Text += ddlDay2.Text;
        }
        txtInsuranceType.Text = ddlInsuranceType.Text;
        txtReportType.Text = ddlReportType.Text;
        grvCarDetail.DataBind();
        
        lblRecordCount.Text = grvCarDetail.Rows.Count.ToString() + " record(s).";        
        lblSelectedCarDetail.Text = "";
    }

    protected void grvCarDetail_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSelectCarDetail();
    }

    protected void grvCarDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            if (Session[CE.Global.General.UserRole] != null && Session[CE.Global.General.UserRole].ToString() == "300")
            {
                e.Row.Cells[4].Visible = true;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
            }
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Session[CE.Global.General.UserRole] != null && Session[CE.Global.General.UserRole].ToString() == "300")
            {
                e.Row.Cells[4].Visible = true;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
            }
        }
    }

    private void GetSelectCarDetail()
    {
        if (grvCarDetail.Rows.Count > 0)
        {
            if (grvCarDetail.SelectedIndex >= 0)
            {
                try
                {
                    txtUserId.Text = grvCarDetail.SelectedRow.Cells[0].Text;
                    lblSelectedCarDetail.Text = "<table width='75%' bgcolor='lightgray'><tr><td colSpan='6'><b>Car Details<br></td></tr>";
                    lblSelectedCarDetail.Text += "<tr><td>Car ID</td>";
                    lblSelectedCarDetail.Text += "<td>User Type</td>";
                    lblSelectedCarDetail.Text += "<td>Email</td>";
                    lblSelectedCarDetail.Text += "<td>Car Make/Model</td>";
                    lblSelectedCarDetail.Text += "<td>CarModel Year</td>";
                    lblSelectedCarDetail.Text += "<td>Date/Time</td></tr>";
                    lblSelectedCarDetail.Text += "<tr><td>" + grvCarDetail.SelectedRow.Cells[1].Text + //Car ID
                                                 "</td><td>" + grvCarDetail.SelectedRow.Cells[3].Text + //User Type
                                                 "</td><td>" + grvCarDetail.SelectedRow.Cells[4].Text + //Email
                                                 "</td><td>" + grvCarDetail.SelectedRow.Cells[7].Text + //Car Make / Model
                                                 "</td><td>" + grvCarDetail.SelectedRow.Cells[8].Text + // CarModel Year
                                                 "</td><td>" + grvCarDetail.SelectedRow.Cells[9].Text + /// Quote Date/Time
                                                 "</td></tr><tr><td colSpan='6'><td></tr></table>";
                }
                catch (Exception Ex)
                {
                    Ex = null;
                    if (grvCarDetail.Rows.Count > 0)
                    {
                        grvCarDetail.SelectedIndex = 0;
                        GetSelectCarDetail();
                        lblSelectedCarDetail.Text = "";
                    }
                }
            }
        }
        else
        {
            lblSelectedCarDetail.Text = "";
        }
    }

    public void ExportGridView(string fileName, GridView gvExport, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;

        Label lblStart = new Label();
        lblStart.Text = "<table style='text-align=left;'><tr><td>";

        Label lblEnd = new Label();
        lblEnd.Text = "</td></tr></table>";
        frm.Controls.Add(lblStart);
        gvExport.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lblSelectedCarDetail);

        Label lblUserProfile = new Label();
        lblUserProfile.Text = "<table><tr><td><b>Profile</b></td></tr></table>";
        frm.Controls.Add(lblUserProfile);
        frm.Controls.Add(grvUserProfile);

        Label lblPolicy = new Label();
        lblPolicy.Text = "<table><tr><td><b>Policy</b></td></tr></table>";
        frm.Controls.Add(lblPolicy);
        frm.Controls.Add(grvCarPolicy);

        Label lblResult = new Label();
        lblResult.Text = "<table><tr><td><b>Results</b></td></tr></table>";
        frm.Controls.Add(lblResult);
        frm.Controls.Add(grvCarResult);

        frm.Controls.Add(lblEnd);
        frm.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        if (grvCarDetail.Rows.Count > 0)
        {
            string _strDateRange = "Date";
            string _ReportType;
            string _strDate1 = txtReportDate1.Text;
            string _strDate2 = txtReportDate2.Text;
            string _Date = "";
            string _UserType = "Both";
            string _FileName = "CarQoutesSummary@";
            if (ddlUserType.Text == "300")
            {
                _UserType = "Provider";
            }
            else if (ddlUserType.Text == "400")
            {
                _UserType = "Customer";
            }


            _ReportType = "";
            if (ddlReportType.Text == "0")
            {
                _ReportType = "Monthly";
                _Date = " : " + GetMonth(_strDate1.Substring(4, 2).ToString());
                _Date += " " + _strDate1.Substring(0, 4).ToString();

                _FileName += GetMonth(_strDate1.Substring(4, 2).ToString()).Trim() + _strDate1.Substring(0, 4).ToString();
            }
            else if (ddlReportType.Text == "1")
            {
                _ReportType = "Daily";
                _Date = " : " + _strDate1.Substring(6, 2).ToString();
                _Date += " " + GetMonth(_strDate1.Substring(4, 2).ToString());
                _Date += " " + _strDate1.Substring(0, 4).ToString();

                _FileName += _strDate1.Substring(6, 2).ToString() + GetMonth(_strDate1.Substring(4, 2).ToString()).Trim() + _strDate1.Substring(0, 4).ToString();
            }
            else if (ddlReportType.Text == "2")
            {
                _ReportType = "Customize";
                _Date = ": " + _strDate1.Substring(6, 2).ToString();
                _Date += " " + GetMonth(_strDate1.Substring(4, 2).ToString());
                _Date += " " + _strDate1.Substring(0, 4).ToString();
                _Date += " TO " + _strDate2.Substring(6, 2).ToString();
                _Date += " " + GetMonth(_strDate2.Substring(4, 2).ToString());
                _Date += " " + _strDate2.Substring(0, 4).ToString();

                _FileName += _strDate1.Substring(6, 2).ToString() + GetMonth(_strDate1.Substring(4, 2).ToString()).Trim() + _strDate1.Substring(0, 4).ToString();
            }

            _strDateRange = "<tr><td>Report Type</td><td>: " + _ReportType + "</td></tr><tr><td>Statement Date</td><td align='left'>" + _Date + "</td></tr>";

            string strHeader = "<table><tr><td><h3>Car Quotes Summary</h3></td>";
            strHeader += "<td style='color:white'>" + CE.Global.General.GetUTCDateTime() + "</td>";
            strHeader += _strDateRange;
            strHeader += "<tr><td>User Type</td><td>: " + _UserType + "</td></tr>";
            strHeader += "<tr><td>Record Count</td><td>: " + lblRecordCount.Text + "</td></tr>";
            strHeader += "<tr><td></td><td></td></tr></table>";

            ExportGridView(_FileName, grvCarDetail, strHeader);
        }
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = "From Date :";
            lblToDate.Text = " To Date :";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (grvCarDetail.Rows.Count > 0)
        {
            if (grvCarDetail.Rows.Count != grvCarDetail.SelectedIndex + 1)
            {
                grvCarDetail.SelectedIndex += 1;
                GetSelectCarDetail();
                btnPrevious.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
                btnPrevious.Enabled = true;
            }
        }
    }

    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        if (grvCarDetail.Rows.Count > 0)
        {
            if ((grvCarDetail.SelectedIndex - 1) >= 0)
            {
                grvCarDetail.SelectedIndex -= 1;
                GetSelectCarDetail();
                btnNext.Enabled = true;
            }
            else
            {
                btnPrevious.Enabled = false;
                btnNext.Enabled = true;
            }
        }
    }

    public decimal ToDecimal(string Value)
    {
        if (Value.Length == 0)
            return 0;
        else
            Value = Value.Replace(",", "");
        Value = Value.Replace("฿", "");
        Value = Value.Replace("R", "");

        return decimal.Parse(Value.Replace(" ", ""));
    }

    public string GetMonthName(string _month)
    {
        string _Month = "";
        if (_month == "01")
        {
            _Month = "January";
        }
        else if (_month == "02")
        {
            _Month = "February";
        }
        else if (_month == "03")
        {
            _Month = "March";
        }
        else if (_month == "04")
        {
            _Month = "April";
        }
        else if (_month == "05")
        {
            _Month = "May";
        }
        else if (_month == "06")
        {
            _Month = "June";
        }
        else if (_month == "07")
        {
            _Month = "July";
        }
        else if (_month == "08")
        {
            _Month = "August";
        }
        else if (_month == "09")
        {
            _Month = "September";
        }
        else if (_month == "10")
        {
            _Month = "October";
        }
        else if (_month == "11")
        {
            _Month = "November";
        }
        else if (_month == "12")
        {
            _Month = "December";
        }
        return _Month;
    }

    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan ";
        }
        else if (month == "02")
        {
            result = " Feb ";
        }
        else if (month == "03")
        {
            result = " Mar ";
        }
        else if (month == "04")
        {
            result = " Apr ";
        }
        else if (month == "05")
        {
            result = " May ";
        }
        else if (month == "06")
        {
            result = " Jun ";
        }
        else if (month == "07")
        {
            result = " Jul ";
        }
        else if (month == "08")
        {
            result = " Aug ";
        }
        else if (month == "09")
        {
            result = " Sep ";
        }
        else if (month == "10")
        {
            result = " Oct ";
        }
        else if (month == "11")
        {
            result = " Nov ";
        }
        else if (month == "12")
        {
            result = " Dec ";
        }
        return result;
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if(PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnExport.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnExport.Enabled = true;
        }
    }

}
